package com.danicsoft.daide.historian;

import com.danicsoft.daide.binary.CONNECTION_STATE;
import com.danicsoft.daide.binary.Connection;

/**
 * Tracks handshaking and changes in connection status
 * 
 * @author Daniel Yule (daniel.yule@gmail.com)
 * @version 1.0
 */
public class BinaryHistorian extends Historian {

	/**
	 * Construct a new BinaryHistorian using a default console output.
	 */
	public BinaryHistorian() {
		super();

	}

	/**
	 * Construct a new <code>BinaryHistorian</code> using the given
	 * <code>Textbook</code> for output.
	 * 
	 * @param textbook
	 *        The textbook to output to.
	 */
	public BinaryHistorian(Textbook textbook) {
		super(textbook);

	}

	/**
	 * Update our connection status with the given message
	 * 
	 * @param state
	 *        The new state of the connection.
	 * @param conn
	 *        The connection whose state has changed.
	 */
	public void logConnectionState(CONNECTION_STATE state, Connection conn) {
		String message = "";
		switch (state) {
			case CONNECTED:
				message = "Handshaking complete.";
				break;
			case CONNECTING:
				message = "Handshaking begun.";
				break;
			case DISCONNECTED:
				message = "Disconnected.";
				break;
			case ERROR:
				message = "Error encountered.";
				break;
			case LISTENING:
				message = "Listening for incoming connections.";
				break;
		}

		logMessage(conn, message, INFO);
	}

	/**
	 * Log an error that we've had with the connection.
	 * 
	 * @param errorMessage
	 *        A message representing the problem.
	 * @param conn
	 *        The connection that generated the error.
	 */
	public void logConnectionError(String errorMessage, Connection conn) {
		logMessage(conn, errorMessage, ERROR);
	}
}
